package com.unknowpupil.design.sigleton;

import java.util.concurrent.CountDownLatch;

/**
 * @Description: <br/>
 * <p>
 * 多线程下测试。
 * CountDownLatch 同步辅助类，在完成一组正贼其他线程中执行操作之前，允许一个或多个线程一致等待。
 * countDown()当钱线程掉此方法。计数减一，建以放在 finally 里执行
 * await() 调用此方法会一致阻塞当前线程，知道计数器为o
 * </p>
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2020/4/24 18:07
 * @Classname: Client2
 * @Version: 1.0
 */
public class Client2 {
    public static void main(String[] args) throws Exception {
        long begin = System.currentTimeMillis();
        int threadNum = 10;
        final CountDownLatch  countDownLatch = new CountDownLatch(threadNum);

        for (int j = 0; j < threadNum; j++) {
            /**
             * 一个线程调用10万次。
             */
            new Thread(new Runnable() {



                @Override
                public void run() {
                    for (int i = 0; i < 1000000; i++) {
                        Object o = LazySingleton.getInstance();
                    }
                    countDownLatch.countDown();
                }
            }).start();
        }
        // main 线程阻塞，知道计数器变为0 才会继续往下执行。
        countDownLatch.await();
        long end = System.currentTimeMillis();

        System.out.println("总耗时： " + (end - begin));
    }


}
